package com.chixing.controller;


import com.chixing.entity.Product;
import com.chixing.service.IProductService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-09-17
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @GetMapping("{id}")
    public Result getById(@PathVariable("id") Integer id){
        return productService.getById(id);
    }

    @GetMapping(value = {"/page/{pageNumber}",""})
    public Result getPage(@PathVariable(value = "pageNumber",required = false) Integer pageNumber){
        if (pageNumber==null)
            pageNumber=1;
        return productService.getPage(pageNumber);
    }
    //String proName, Float proMinPrice, Float proMaxPrice, String proType
    @GetMapping("/pro")
    public Result getPro(@RequestParam(value = "proName",required = false) String proName,
                         @RequestParam(value = "proMinPrice",required = false) Float proMinPrice,
                         @RequestParam(value = "proMaxPrice",required = false) Float proMaxPrice,
                         @RequestParam(value = "proType",required = false) String proType){
        return productService.getPro(proName, proMinPrice, proMaxPrice, proType);
    }

    @PostMapping
    public Result save(Product product){
        return productService.save(product);
    }
    @PutMapping
    public Result update(Product product){
        return productService.update(product);
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer id){
        return productService.delete(id);
    }

}
